package org.truenewx.tnxjee.core.enums;

import java.util.*;

import org.apache.commons.lang3.StringUtils;
import org.truenewx.tnxjee.core.util.tuple.Binary;
import org.truenewx.tnxjee.core.util.tuple.Binate;

/**
 * 枚举字典
 *
 * @author jianglei
 */
public class EnumDictImpl extends EnumDict {

    private Map<Binate<String, String>, EnumType> types = new HashMap<>();

    public EnumDictImpl(Locale locale) {
        super(locale);
    }

    @Override
    public void addType(EnumType type) {
        if (type != null) {
            Binate<String, String> key = getKey(type.getName(), type.getSubname());
            this.types.put(key, type);
        }
    }

    private Binate<String, String> getKey(String name, String subname) {
        if (StringUtils.isBlank(subname)) {
            subname = null;
        }
        return new Binary<>(name, subname);
    }

    @Override
    public EnumType getType(String name, String subname) {
        Binate<String, String> key = getKey(name, subname);
        return this.types.get(key);
    }

    @Override
    public boolean containsType(String name, String subname) {
        Binate<String, String> key = getKey(name, subname);
        return this.types.containsKey(key);
    }

    @Override
    public int getTypeTotal() {
        return this.types.size();
    }

    @Override
    public List<EnumType> getSubTypes(String name) {
        List<EnumType> types = new ArrayList<>();
        for (EnumType type : this.types.values()) {
            if (type.getName().equals(name) && StringUtils.isNotBlank(type.getSubname())) {
                types.add(type);
            }
        }
        return types;
    }

}
